import { resourceLibraryFileMock } from './data/library.file'
import { getNowDateTime } from '../../utils'
let resourceLibraryFileList = resourceLibraryFileMock(40)
export default [
  {
    url: '/infra/resource-library-recycle/return', // 文件恢复
    method: 'post',
    response: data => {
      returnFile(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/infra/resource-library-recycle/delete' + '.*'), // 删除
    method: 'delete',
    response: data => {
      deleteByIds(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/infra/resource-library-recycle/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/infra/resource-library-recycle/count' + '.*'), // 获取回收站文件总数
    method: 'get',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getFileCount()
      }
    }
  }
]

const getFileCount = () => {
  let result = resourceLibraryFileList
  result = result.filter(item => !item.deleted)
  return result.length
}

const getListByPage = params => {
  let result = resourceLibraryFileList
  result = result.filter(item => !item.deleted)
  if (params.query.name) {
    result = result.filter(item => item.name.indexOf(params.query.name) !== -1)
  }
  if (params.query.mimetype) {
    result = result.filter(item => params.query.mimetype === item.mimetype)
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const returnFile = data => {
  resourceLibraryFileList.forEach(item => {
    const hasItem = data.body.ids.some(v => {
      return v === item.id
    })
    if (hasItem) {
      item.deleted = true
      item.updater = '1'
      item.update_time = getNowDateTime()
      item.deleted_time = getNowDateTime()
    }
  })
}

const deleteByIds = data => {
  resourceLibraryFileList.forEach(item => {
    const hasItem = data.body.ids.some(v => {
      return v === item.id
    })
    if (hasItem) {
      item.deleted = true
      item.updater = '1'
      item.update_time = getNowDateTime()
      item.deleted_time = getNowDateTime()
    }
  })
}
